<template>
	<view class="main">
		<u-search placeholder="请输入身份证号" @search="search" @custom="search" v-model="keyword"></u-search>
		
		<u-form label-width="160"  :label-style="{'font-weight':'bold'}">
			<u-form-item :label-position="labelPosition" label="姓ㅤㅤ名:" >
				<u-input :border="border" placeholder=" " v-model="model.laborName" type="text" :disabled="true">
				</u-input>
			</u-form-item>
			
			<u-form-item :label-position="labelPosition" label="身份证号:">
				<u-input :border="border" placeholder=" " v-model="model.idCard" type="text" :disabled="true">
				</u-input>
				<view class="" v-model="model.idCard"></view>
			</u-form-item>
			<u-form-item :label-position="labelPosition" label="原常住地:">
				<u-input :border="border" placeholder=" " v-model="model.oftenArea" type="textarea" :disabled="true">
				</u-input>
			</u-form-item>
			
			<u-form-item :label-position="labelPosition" label="新常住地:">
				<u-input :border="border" type="select" :select-open="addrpickerShow1" v-model="model.changzhuAddr"
					placeholder="新常住地" @click="addrpickerShow1 = true"></u-input>
			</u-form-item>
			<!-- <u-picker mode="region" v-model="addrpickerShow1" @confirm="regionConfirm2" :area-code="defultArr"></u-picker> -->
			<u-picker mode="region" v-model="addrpickerShow1" @confirm="regionConfirm2" :area-code='defultArr'></u-picker>
							
			<u-form-item :label-position="labelPosition" label="镇(乡,街道):" v-show="liveShow2">
				<u-input :border="border" type="select" :select-open="liveCountryShow" v-model="model.liveCountry"
					placeholder="请选择常住地-镇（乡、街道）" @click="liveCountryShow = true" ></u-input>
			</u-form-item>
			<u-select mode="single-column" :list="liveCountryList" v-model="liveCountryShow"
				@confirm="FnConfirm('liveCountry', $event)"></u-select>
				
			<u-form-item :label-position="labelPosition" label="镇(乡,街道)" v-show="liveShow1">
				<u-input :border="border" placeholder="请输入常住地-镇（乡、街道）" v-model="model.liveCountry" type="text">
				</u-input>
			</u-form-item>
				
			
			<u-form-item :label-position="labelPosition" label="村(居,社区):" v-show="liveShow2">
				<u-input :border="border" type="select" :select-open="liveVillageShow" v-model="model.liveVillage"
					placeholder="请选择常住地-村（居、社区）" @click="liveVillageShow = true" ></u-input>
			</u-form-item>
			<u-select mode="single-column" :list="liveVillageList" v-model="liveVillageShow"
				@confirm="FnConfirm('liveVillage', $event)"></u-select>
				
			<u-form-item :label-position="labelPosition" label="村(居,社区):" v-show="liveShow1">
				<u-input :border="border" placeholder="请输入常住地-村（居、社区）" v-model="model.liveVillage" type="text">
				</u-input>
			</u-form-item>
			
			<!-- 常住地小组或网格 -->
			<u-form-item :label-position="labelPosition" label="网格名称:">
				<u-input :border="border" placeholder="请输入新常住地小组或网格" v-model="model.grid" type="text">
				</u-input>
			</u-form-item>
		</u-form>
		
		<view class="btns">
			<u-button class="sub" type="primary" @click="submit">确认迁移</u-button>
		</view>
	</view>
</template>

<script>
	import api from "../../api/routes.js";
	export default{
		data(){
			return{
				keyword:'',
				list:[],
				// defultArr:[52,5201],
				defultArr:[52,5201,520102],
				addrpickerShow1:false,
				liveCountryShow:false,
				liveVillageShow:false,
				liveCountryList:[], //常住地-镇
				liveVillageList:[], //常住地-村
				liveShow1: true,
				liveShow2: false,
				model:{
					id:'',
					changzhuAddr:'',
					liveProvince:'',
					liveCity:'',
					liveArea:'',
					liveCountry:'',
					liveVillage:'',
					grid:'',
					oftenArea:'',//常住地
					laborName:'',
					idCard:'',
				}
			}
		},
		// onLoad() {
		// 	this.liveCountry1()
		// 	this.liveVillage1()
			
		// },
		methods:{
			submit(){
				console.log(this.model,'this.model');
				// 防空判断
				if (this.keyword.length <= 0) {
				    uni.showToast({
				      icon: "none",
				      title: "请输入身份证号",
				    });
				    return;
				}
				if (this.list.length <= 0) {
				    uni.showToast({
				      icon: "none",
				      title: "请点击搜索身份证号",
				    });
				    return;
				}
				
				if (this.model.changzhuAddr.length <= 0) {
				    uni.showToast({
				      icon: "none",
				      title: "请选择常住地省市区",
				    });
				    return;
				}
				
				if (this.model.liveCountry.length <= 0) {
				    uni.showToast({
				      icon: "none",
				      title: "请选择常住地-镇",
				    });
				    return;
				}
				
				if (this.model.liveVillage.length <= 0) {
				    uni.showToast({
				      icon: "none",
				      title: "请选择常住地-村",
				    });
				    return;
				}
				if(this.model.liveProvince==this.list.liveProvince && this.model.liveProvince==this.list.liveProvince && this.model.liveArea==this.list.liveArea && this.model.liveCountry==this.list.liveCountry && this.model.liveVillage==this.list.liveVillage){
					uni.showToast({
					  icon: "none",
					  title: "迁移原常住地不能和新常住地一致",
					});
					return;
				}
				
				
				let that=this;
				wx.showModal({
				  title: '提示',
				  content: '您确定要迁移吗？',
				  success (res) {
				    if (res.confirm) {
				      console.log('用户点击确定') 
					 that.$myRequest({
					 			url: api.dataMigration,
					 			method: "POST",
					 			data: { 
					 				id:that.list.id,
					 				liveProvince:that.model.liveProvince,
					 				liveCity:that.model.liveCity,
					 				liveArea:that.model.liveArea,
					 				liveCountry:that.model.liveCountry,
					 				liveVillage:that.model.liveVillage,
					 				grid:that.model.grid,
					 			},
					 		}).then(res => {
					 			
					 			if(res.data.code==0){
									uni.showToast({
									  icon: "none",
									  title: res.data.msg,
									});
					 				// that.$u.toast(res.msg)
									setTimeout(() => {
										wx.navigateBack({
										  delta: 1
										})
										wx.hideLoading()
									}, 2000)
									that.model=[];
									that.list=[];
									that.keyword='';
									that.oftenArea='';
					 			}
								
					 			
					 		})
					  
					  
					  
					  
				    } else if (res.cancel) {
				      console.log('用户点击取消')
				    }
				  }
				})
			},
			
			async search(value){
				if (this.parseId(value)) {
					this.$u.toast('请输入正确的身份证号')
					return
				}
				const res = await this.$myRequest({
					url: api.getByIdCard,
					method: "GET",
					data: {
						idCard: value
					},
				});
				this.list=res.data.data;
				this.model.oftenArea=res.data.data.liveProvince+"-"+res.data.data.liveCity+"-"+res.data.data.liveArea+"-"+res.data.data.liveCountry+"-"+res.data.data.liveVillage;
				this.model.laborName=res.data.data.laborName;
				this.model.idCard=res.data.data.idCard;
				
				console.log(res,'res');
				console.log(this.model.oftenArea,'原常住地');
				console.log(this.model,'model');
				
				console.log(res,'数据迁移');
			},
			// 判断是否输入正确身份证号
			parseId(pId) {
				var arrVerifyCode = [1, 0, "x", 9, 8, 7, 6, 5, 4, 3, 2];
				var wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
				if (pId.length != 15 && pId.length != 18)
					return true;
				var ai = (pId.length == 18) ? pId.substr(0, 17) : pId.substr(0, 6) + "19" + pId.substr(6);
				if (!/^\d+$/.test(ai))
					return true;
				var yyyy = ai.substr(6, 4),
					mm = ai.substr(10, 2) - 1,
					dd = ai.substr(12, 2);
				var d = new Date(yyyy, mm, dd),
					year = d.getFullYear(),
					mon = d.getMonth(),
					day = d.getDate(),
					now = new Date();
				if (year != yyyy || mon != mm || day != dd || d > now || now.getFullYear() - year > 140)
					return true;
				for (var i = 0, ret = 0; i < 17; i++) ret += ai.charAt(i) * wi[i];
				ai += arrVerifyCode[ret %= 11];
				if (pId.length == 18 && pId.toLowerCase() != ai)
					return true;
			
				return false;
			},
			
			async liveCountry1(){
				// 查询镇:   app/govLabor/getDept?pid=1067246875800000066 
				const res =await this.$myRequest({
					url: api.queryTown,
					method: "GET",
					data: {
						pid:"1067246875800000066"
					
					},
				});			
				const list1 = res?.data?.list?.map(s => {
					return {
						value: s.name,
						label: s.name,
						id:s.id
					}
				})
				this.liveCountryList=list1
				console.log(res,3636);
				console.log(this.liveCountryList,2626);
			},
			
			async liveVillage1(pid){
				// 查询村:   app/govLabor/getDept?pid=所选镇的ID
				console.log(pid,'IIIIIII');
				const res =await this.$myRequest({
					url: api.queryTown,
					method: "GET",
					data: {
						pid
					
					},
				});
				
				const list1 = res?.data?.list?.map(s => {
					return {
						value: s.name,
						label: s.name,
						id:s.id
					}
				})
				this.liveVillageList=list1
				console.log(res,4545);
				console.log(this.liveVillageList,5252);
			},
			regionConfirm2(e) {
			
				this.model.liveProvince = e.province.label
				this.model.liveCity = e.city.label
				this.model.liveArea = e.area.label
				this.model.changzhuAddr =
				e.province.label + "-" + e.city.label + "-" + e.area.label;
				// 选择器打开就显示已选中地区
				this.defultArr=[e.province.value,e.city.value,e.area.value]				
				console.log(this.defultArr,'this.defultArr222');
				
				
				if(e.province.value==52 && e.city.value==5202 && e.area.value==520201){
					console.log('六盘水市中山区');
					this.liveCountry1()
					this.liveVillage1()
					this.liveShow2=true;
					this.liveShow1=false;
					
				}else{
					this.liveShow2=false;
					this.liveShow1=true;
				}
				
				console.log(e,'新常住地！！');
			
			},
			FnConfirm(val, name, type) {
				console.log(val, name,type ,9999999999);
			
				if (type) {
					this.model[type] = name[0].value
				} else {
					this.model[val] = name[0].value
				}
				if(val=='liveCountry'){
					let id = this.getLiveCountryId(name[0].value);
					console.log(id, 'z-------');
					this.liveVillage1(id)
				}				
			},
			getLiveCountryId(name){
				let id = null;
				for (var i = 0; i < this.liveCountryList.length; i++) {
					let item =  this.liveCountryList[i];
					if(item.value==name){
						id = item.id;
						break;
					}
			 }
				return id;
			},
				
		},
	}
</script>

<style lang="scss" scoped>
	.main{
		padding: 10px;
		.ulMain{
			margin: 10px 0 0 0;
			li{
				height: 40px;
				line-height: 40px;
				span{
					font-weight: bold;
				}
			}
			.ulLi3{
				width: 100%;
				height: 50px;
				line-height: 24px;
				// display: -webkit-box; //（兼容性）
				
				// -webkit-line-clamp:2;//（指定显示行数）
				
				// -webkit-box-orient: vertical;//（文本垂直排列）
				
				// overflow: hidden; //（多余部分隐藏）
				
			}
		}
		
		
	   .btns {
	   	// box-shadow: 0 0 8px #cccccc;
	   	height: 70px;
	   	display: flex;
	   	align-items: center;
	   	justify-content: center;
	   	// position: fixed;
	   	bottom: 0px;
	   	left: 0;
	   	right: 0;
	   	z-index: 999;
	   	// background: #fff;
	   
	   	.sub {
	   		display: block;
	   		width: 100%;
	   		margin: 0 auto;
	   	}
	   }
	}
</style>